package com.syedu.cels.controller;

import com.syedu.cels.entity.Vocabulary;
import com.syedu.cels.entity.Word;
import com.syedu.cels.service.BaseService;
import com.syedu.cels.util.BeanFactory;
import com.syedu.cels.util.InputUtil;
import com.syedu.cels.util.PageBean;
import com.syedu.cels.view.UI;

import java.util.List;
import java.util.Map;

/**
 * description：启动器
 * @author Administrator
 * @date 2021/9/29 14:45
 */
public class App {
    public static void main(String[] args) {
        BaseService service = (BaseService) BeanFactory.getBean("baseService");
        UI ui = new UI();
        ui.title();
        boolean flag = true;
        try {
            while (flag) {
                ui.mainMenu();
                int choice = InputUtil.getInt("请选择> ");
                switch (choice) {
                    case 1:
                        ui.showDatabase();
                        choice = InputUtil.getInt("请选择> ");
                        switch (choice) {
                            case 1:
                                Map<Character, List<Word>> map = service.showWordsByMap();
                                service.showWordsMap(map);
                                String inputStr = InputUtil.getStr("请选择要查看的首字母> ").toUpperCase();
                                List<Word> subList = map.get(inputStr.charAt(0));
                                long count = 1;
                                for (int i = 0; i<subList.size();i++){
                                    subList.get(i).setId(count++);
                                }
                                PageBean<Word> pageBean = null;
                                int pageNow = 1;
                                while (true) {
                                    pageBean = service.findWordsByPage(subList, pageNow++, 15);
                                    pageBean.getDatas().forEach(System.out::println);
                                    inputStr = InputUtil.getStr("是否查看下一页[y/n]? ");
                                    if (!"y".equalsIgnoreCase(inputStr)) {
                                        break;
                                    }
                                    if (pageNow> pageBean.getTotalPages()){
                                        System.out.println("已显示完毕");
                                        break;
                                    }
                                }

                                break;
                            case 2:
                                PageBean<Vocabulary> pageBean1 = null;
                                int inputInt = 1;
                                pageBean1 = service.findVocabulariesByPage(inputInt, 15);
                                while (true) {
                                    System.out.println("每页[" + 15 + "]行" + "共计["
                                            + pageBean1.getTotalPages() + "]页，词汇总数量[" + pageBean1.getCounts() + "]\t" +
                                            "返回上一级  0");
                                    System.out.println(" | " + pageBean1.getPageNow() + " | " + (pageBean1.getPageNow() + 1) + " | " +
                                            (pageBean1.getPageNow() + 2) + " | " + (pageBean1.getPageNow() + 3) + " | " +
                                            "....| " + pageBean1.getTotalPages() + " | ");
                                    inputInt = InputUtil.getInt(("请选择页码[1-" + pageBean1.getTotalPages() + "] > "));
                                    pageBean1 = service.findVocabulariesByPage(inputInt, 15);
                                    if(inputInt == 0){
                                        break;
                                    }
                                    pageBean1.getDatas().forEach(System.out::println);
                                }


                            case 0:
                                break;
                        }
                    case 2:
                        boolean en = false;
                        String in = InputUtil.getStr("是否继续上次？ 输入y/n");
                        if(in.equals("y")){
                            en = true;
                        }
                        service.en2cn(en);

                        break;
                    case 0:
                        System.out.println("~谢谢使用~");
                        flag = false;
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}